package br.com.wuo.karina.view;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import br.com.wuo.karina.model.Turma;
import br.com.wuo.karina.model.dao.impl.AlunoDaoImpl;
import br.com.wuo.karina.model.dao.impl.TurmaDaoImpl;

public class AtribuirTurma extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Integer> codAlunos = new ArrayList<Integer>();
	private JTextField txtAlunos;
	private JComboBox<String> cmbTurmas, cmbAlunos;
	private MigLayout layout;
	private JButton btnSave, btnClear, btnExit, btnAdicionar, btnExcluir;
	private TurmaDaoImpl turmaDao;
	private AlunoDaoImpl alunoDao;
	
	
	public AtribuirTurma(){
		super ("Atribuir Turma");
		initialize();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        addListener();
		
	}	
	
	public void initialize() {
        layout = new MigLayout("", "60px[100][100][100]80px", "");
        setLayout(layout);
        
        turmaDao = new TurmaDaoImpl();
        alunoDao = new AlunoDaoImpl();
                
        txtAlunos = new JTextField();
        
        cmbAlunos = new JComboBox<String>();
        cmbTurmas = new JComboBox<String>();
        
        populateCombos();
        
        btnSave = new JButton("Salvar");
        btnClear = new JButton("Limpar");
        btnExcluir = new JButton("Excluir");
        btnAdicionar = new JButton("Adicionar");
        btnExit = new JButton("Sair");
        
        add(new JLabel("Turma: "));
        add(cmbTurmas, "w 230!, wrap 15");
        add(new JLabel("Aluno:"));
        add(cmbAlunos, "grow");
        add(btnAdicionar, "grow, wrap");
        add(txtAlunos, "span2, grow, wrap 20");
        
        add(btnSave, "split 4, span 3, align 50% 50%");
        add(btnExcluir);
        add(btnClear);
        add(btnExit);
        pack();
	
	}
	
	private void addListener() {
		btnSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String item = cmbTurmas.getSelectedItem().toString();
					String[] itemsParts  = item.split("-");
					int codigo = Integer.parseInt(itemsParts[0].trim());
					
					Turma turma = new Turma(codigo, codAlunos);
					turmaDao.AtribuirAlunos(turma);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}				
			}
		});
		
		btnAdicionar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String aluno = cmbAlunos.getSelectedItem().toString();
				String[] itemsParts  = aluno.split("-");
				int codigo = Integer.parseInt(itemsParts[0].trim());
				
				if(!codAlunos.contains(codigo)){
					codAlunos.add(codigo);
					if(txtAlunos.getText().equals("")){
						txtAlunos.setText(aluno);
					}else{
						txtAlunos.setText(txtAlunos.getText() + ", " + aluno);
					}	
				}
			}
		});
		
		btnExcluir.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				try {
					String item = cmbTurmas.getSelectedItem().toString();
					String[] itemsParts  = item.split("-");
					int codigo = Integer.parseInt(itemsParts[0].trim());
					turmaDao.DeletarTurma(codigo);
					codAlunos.clear();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		
		btnClear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				clear();
				
			}
		});
		
		btnExit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();				
			}
		});
		
		cmbTurmas.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				try {
					
					String item = cmbTurmas.getSelectedItem().toString();
					String[] itemsParts  = item.split("-");
					int codigo = Integer.parseInt(itemsParts[0].trim());
					ArrayList<String> alunos = turmaDao.getAlunosDaTurma(codigo);
					
					txtAlunos.setText("");
					codAlunos.clear();
					for(String aluno:alunos){
						
						String[] alunoParts  = aluno.split("-");
						int codigoAluno = Integer.parseInt(alunoParts[0].trim());
						codAlunos.add(codigoAluno);
						
						if(txtAlunos.getText().equals("")){
							txtAlunos.setText(aluno);
						}else{
							txtAlunos.setText(txtAlunos.getText() + ", " + aluno);
						}	
					}
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		
	};
	
	public void populateCombos(){
		try {
			ArrayList<Turma> turmas = turmaDao.getAllTurmas();
			
			for(Turma turma:turmas){
				String itemTurma = turma.getCodigo() + " - " + turma.getNome();
				
				cmbTurmas.addItem(itemTurma);
			}
			
			ArrayList<String> alunos = alunoDao.getAllAlunos();
			
			for(String aluno:alunos){
				cmbAlunos.addItem(aluno);
			}
			
			cmbTurmas.setSelectedIndex(-1);
			cmbAlunos.setSelectedIndex(-1);
						
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Não Encontrado");
		}
	}
	
	public void clear(){
		txtAlunos.setText("");
		codAlunos.clear();
	}

}
